import json
from collections import OrderedDict

from django.shortcuts import render

# Create your views here.
from django.views import View

# 提供首页广告页面
from contents.models import ContentCategory,Content
from goods.models import GoodsChannel,GoodsCategory,GoodsChannelGroup


class IndexView(View):
    def get(self,request):
        # 查询并展示商品分类
        categories=OrderedDict() #创建一个有序的字典，根据key进行排序
        # 查询所有的商品频道：37个一级类别,并根据group_id,sequence进行排序
        channels=GoodsChannel.objects.order_by('group_id','sequence')
        # 遍历
        for channel in channels:
            # 获取当前频道所在的组
            group_id=channel.group_id
            # 构造基本的数据框架：只有11个组
            if  group_id not in categories:
                categories[group_id]={'channels':[],'sub_cats':[]}

            # 查询当前频道对应的一级类别
            cat1=channel.category
            # 将cat1添加到channels中
            categories[group_id][channels].append({
                'id':cat1.di,
                'name':cat1.name,
                'url':cat1.url,
            })
            # 查询二级和三级类别
            for cat2 in cat1.subs.all():#从一级类别找出二级类别
                cat2.sub_cats=[] #给二级类别添加一个可以保存三级类别的列表
                for cat3 in cat2.subs.all():#从二级类别找出三级类别
                    cat2.sub_cats.append(cat3)#将三级类别添加到二级sub_cats
                # 将二级类别添加到一级sub_cats
                categories[group_id]['sub_cats'].append(cat2)

        #查询首页广告
        # 查询所有的广告类别
        contents=OrderedDict()
        content_categories=ContentCategory.objects.all()
        for content_category in content_categories:
            contents[content_category.key]=content_category.content_set.filter(status=True).order_by('sequence')

        context={
            'categories':categories,
            'contents':contents
        }
        return  render(request,'index.html',context)